package org.dromara.school.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.dromara.common.core.enums.SchoolType;
import org.dromara.common.core.exception.ExcelException;
import org.dromara.common.core.utils.SchoolUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.school.domain.Campus;
import org.dromara.school.domain.Classes;
import org.dromara.school.domain.Student;
import org.dromara.school.domain.bo.StudentBo;
import org.dromara.school.domain.vo.StudentVo;
import org.dromara.school.service.ICampusService;
import org.dromara.school.service.IClassesService;
import org.dromara.school.service.IStudentService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description //TODO
 * @Date 2025/6/20 14:16
 * @Author 郭建波
 **/
public class StudentImportExcelListener extends AnalysisEventListener<StudentVo> {



    private IStudentService studentService;
    private ICampusService schoolCampusService;
    private IClassesService classesService;


    //定义存放数据的集合
    private List<StudentVo> list = new ArrayList<>();

    public StudentImportExcelListener(IStudentService studentService, ICampusService schoolCampusService, IClassesService classesService) {
        this.studentService = studentService;
        this.schoolCampusService = schoolCampusService;
        this.classesService = classesService;
    }

    // 一行行读取内容
    @Override
    public void invoke(StudentVo studentVo, AnalysisContext analysisContext) {
        StudentVo getStudentDto = this.checkNull(studentVo, analysisContext);

        list.add(getStudentDto);
    }


    // 读取表头内容
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }

    // 读取完成之后的操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        list.forEach(stu -> {
            StudentBo studentBo = new StudentBo();
            studentBo.setSchoolCampusId(stu.getSchoolCampusId());
            studentBo.setClassesId(stu.getClassesId());
            studentBo.setStudentName(stu.getStudentName());
            studentBo.setClassYear(stu.getClassYear());
            studentBo.setClassNum(stu.getClassNum());
            studentBo.setSex(stu.getSex());
            studentBo.setStudentCode(Long.valueOf(stu.getStudentCode()));
            studentBo.setStudentNumber(Long.valueOf(stu.getStudentNumber()));
            studentBo.setStudentStatus(stu.getStudentStatus());
            studentBo.setNation(stu.getNation());
            studentBo.setContact(stu.getContact());
            studentBo.setDistricts(stu.getDistricts());
            studentBo.setCity(stu.getCity());
            studentBo.setProvince(stu.getProvince());
            this.studentService.insertByBo(studentBo);
        });


    }


    //判断每一行必填数据，返回校区id
    public StudentVo checkNull(StudentVo studentVo, AnalysisContext context) {
        List<Integer> listcampusId = new ArrayList<>();
        int row = context.readRowHolder().getRowIndex() + 1;
        String campusName = studentVo.getCampusName();
        String className = studentVo.getClassName();
        Long sex = studentVo.getSex();
        Long studentCode = studentVo.getStudentCode();
        Long studentNumber = studentVo.getStudentNumber();
        if (StringUtils.isEmpty(campusName) ||
            StringUtils.isEmpty(className) ||
            StringUtils.isEmpty(studentVo.getStudentName()) ||
            studentCode == null||
            studentNumber == null ||
            sex == null) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  出现导入必须要的数据为空,请检查！");
        }
        Campus schoolCampus = new Campus();
        schoolCampus.setCampusName(campusName);
        //验证校区是否存在
        Campus getSchoolCampus = schoolCampusService.findSchoolCampus(schoolCampus);
        if (StringUtils.isNull(getSchoolCampus)) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  导入的校区没有找到,请检查！");
        }
        studentVo.setSchoolCampusId(getSchoolCampus.getCampusId());
        //验证班级是否存在
        // 通过班级名字来匹配对应年份
        Map<String, Integer> schoolClassName = SchoolUtils.getSchoolClassName(SchoolType.SYSTEM_OF_NINEYEAR.getType());
        Integer getyear = schoolClassName.get(SchoolUtils.getclass(className));
        if (getyear == null) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  导入的班级没有找到,请检查！");
        }
        studentVo.setClassYear(getyear);
        Integer getnum = SchoolUtils.getclassNum(className);
        if (getnum == null) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  导入的班级没有找到,请检查！");
        }
        studentVo.setClassNum(getnum);
        //通过班级年份，校区去班级表中查询
        Classes c = new Classes();
        c.setClassNum(getnum);
        c.setClassYear(getyear);
        c.setSchoolCampusId(getSchoolCampus.getCampusId());
        Classes getclasses = this.classesService.findClassesList(c);
        if (getclasses == null) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  导入的班级没有找到,请检查！");
        }
        studentVo.setClassesId(getclasses.getId());
        //验证性别
        Integer rsex = sex.equals("男") ? 0 : sex.equals("女") ? 1 : 2;
        if (rsex == 2) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行  导入的性别有误,请检查！");
        }
        studentVo.setSex(Long.valueOf(rsex));
        studentVo.setStudentCode(Long.valueOf(studentCode));
        //用学籍号判断学生是否已经存在
        Student student = this.studentService.findStudent(studentVo);
        if (student != null) {
            throw new ExcelException(context.readSheetHolder().getSheetName() + "：第" + row + "行 导入的学生已经存在，请检查！");
        }


        return studentVo;
    }


}
